package com.zr.repertory.warearea.controller;

import com.zr.config.ErrorMessage;
import com.zr.enumvo.StatusEnumSub;
import com.zr.enumvo.TypeEnum;
import com.zr.enumvo.WarehouseMarkEnum;
import com.zr.enumvo.WarehouseStatusEnum;
import com.zr.inquiry.pojo.StatusVo;
import com.zr.planclass.pojo.PlanClassUpdateVo;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import com.zr.warehouse.pojo.*;
import com.zr.warehouse.service.WareAreaService;
import com.zr.warehouse.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HXH on 2019/11/21 0021.
 */
@RestController
@CrossOrigin
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private WareAreaService wareAreaService;

    //分页查询库房
    @PostMapping("warehouse/queryPage")
    public ResultVo queryPage( @RequestBody WarehouseSelectVo warehouseSelectVo){
        ResultVo resultVo =  warehouseService.queryPage(warehouseSelectVo);
        return resultVo;
    }
    //添加库房
    @PostMapping("warehouse/add")
    public ResultVo addWarehouse(@RequestBody @Valid WarehouseAddVo warehouseAddVo, BindingResult bindingResult){
    //判断是否存在错误信息
    if (bindingResult.hasErrors()){
        //如果存在return给页面错误信息
        return ResultBuilderVo.error("500",bindingResult.getFieldError().getDefaultMessage());
    }
    //如果没有错误跳到service层进行逻辑处理
    return warehouseService.addWarehouse(warehouseAddVo);
}

    /*修改回显*/
    @GetMapping("warehouse/beforeupdatewarehouse")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo beforeupdatewarehouse(Integer warehouseID){
        if(warehouseID == null){
            return ResultBuilderVo.error(ErrorMessage.QUERYBYWAREID);
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo =  warehouseService.beforeupdatewarehouse(warehouseID);
        return resultVo;
    }

    /**
     * 修改功能
     * @param warehouseUpdateVo
     * @param bindingResult
     * @return
     */
    @PostMapping("warehouse/update")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo update(@RequestBody @Valid WarehouseUpdateVo warehouseUpdateVo, BindingResult bindingResult){
        //判断是否存在错误信息
        if (bindingResult.hasErrors()){
            //如果存在return给页面错误信息
            return ResultBuilderVo.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo =  warehouseService.update(warehouseUpdateVo);
        return resultVo;
    }
    //分页查询库区
    @PostMapping("storageSection/queryPage")
    public ResultVo queryAreaPage(@RequestBody WareAreaSelectVo wareAreaSelectVo){
        ResultVo resultVo=wareAreaService.queryAreaPage(wareAreaSelectVo);
        return resultVo;
    }


    //添加库区
    @PostMapping("warehouse/addWareArea")
    public ResultVo addWareArea(@RequestBody @Valid WareAreaAddVo wareAreaAddVo, BindingResult bindingResult){
        //判断是否存在错误信息
        if (bindingResult.hasErrors()){
            //如果存在return给页面错误信息
            return ResultBuilderVo.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        return wareAreaService.addWareArea(wareAreaAddVo);
    }

    @PostMapping("iwm/getPullDownList")
    public ResultVo queryXiaLa(){
        Map<String,List<StatusVo>> xialaMap = new HashMap<>();


        //状态
        List<StatusVo> statusVoList = new ArrayList<>();
        //遍历枚举
        for (WarehouseStatusEnum s : WarehouseStatusEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setValue(s.getWarehouseStatusValue());
            statusVo.setLabel(s.getWarehouseStatusName());
            statusVoList.add(statusVo);
        }
        xialaMap.put("statusLists",statusVoList);

        //询价类型
        List<StatusVo> markList = new ArrayList<>();
        //遍历枚举
        for (WarehouseMarkEnum t : WarehouseMarkEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setLabel(t.getWarehouseMarkStatusName());
            statusVo.setValue(t.getWarehouseMarkStatus());
            markList.add(statusVo);
        }
        xialaMap.put("type",markList);
        return ResultBuilderVo.success(xialaMap);
    }

    /**
     * 库区业务属性
     * @return
     */
    /*@GetMapping("storageSection/queryBusinessAttribute")
    public ResultVo queryBusinessAttribute(){

    }*/
}
